package com.example.hewei.smarttransport.Dialog;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.example.hewei.smarttransport.R;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 何伟 on 2017/3/7.
 */

public class SettingDialog extends BaseDialog {
    private EditText mIp1ET;
    private EditText mIp2ET;
    private EditText mIp3ET;
    private EditText mIp4ET;
    private Spinner mLanguageSP;
    private Button mCloseBtn;
    private Button mConfirmBtn;
    private Locale mLocale;
    private  int mSelectedLanguage;

    /**
     * 构造函数
     *
     */
    public SettingDialog() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_config, container, false);
    }

    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);
        initView();
        setAdapter();
        setListener();
        setValue();
    }

    private void setAdapter() {
        String[] mItems=getResources().getStringArray(R.array.languages);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item,mItems);
        mLanguageSP.setAdapter(adapter);

    }

    /**
     * 显示Ip地址
     */
    private void setValue() {
        String ipStr[] = mApp.getServerIpStr().split("\\.");
        if (ipStr.length >= 4) {
            mIp1ET.setText(ipStr[0]);
            mIp2ET.setText(ipStr[1]);
            mIp3ET.setText(ipStr[2]);
            mIp4ET.setText(ipStr[3]);
        }
        if(mLocale.equals(Locale.CHINA)) {
            mLanguageSP.setSelection(0);
            mSelectedLanguage=0;
        }
        else {
            mLanguageSP.setSelection(1);
            mSelectedLanguage=1;
        }
    }

    /**
     * 设置监听器
     *
     */
    private void setListener() {
        mCloseBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SettingDialog.this.dismiss();
            }
        });
        mConfirmBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                setIP();
                setLanguage();
                SettingDialog.this.dismiss();
            }
        });
        mLanguageSP.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mSelectedLanguage=position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void setLanguage() {
        Resources res = getResources();
        Configuration config = res.getConfiguration();
        String s=Locale.CHINA.getLanguage();
        s=Locale.ENGLISH.getLanguage();
        Locale a=mLocale;
        Locale b=Locale.CHINA;
        Locale c=Locale.US;
        if(mLocale.equals(Locale.CHINA)&&mSelectedLanguage==1) {
            config.locale = Locale.ENGLISH;
            res.updateConfiguration(config, res.getDisplayMetrics());
            getActivity().sendBroadcast(new Intent("language"));
        }
        else if(mLocale.equals(Locale.US)&&mSelectedLanguage==0) {
            config.locale = Locale.CHINA;
            res.updateConfiguration(config, res.getDisplayMetrics());
            getActivity().sendBroadcast(new Intent("language"));
        }
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mIp1ET = (EditText) getView().findViewById(R.id.ip1);
        mIp2ET = (EditText) getView().findViewById(R.id.ip2);
        mIp3ET = (EditText) getView().findViewById(R.id.ip3);
        mIp4ET = (EditText) getView().findViewById(R.id.ip4);
        mLanguageSP=(Spinner)getView().findViewById(R.id.language);
        mCloseBtn = (Button) getView().findViewById(R.id.close);
        mConfirmBtn = (Button) getView().findViewById(R.id.confirm);
        mLocale=getResources().getConfiguration().locale;
    }

    /**
     * 对用户输入的Ip进行转化、校验和保存
     *
     */
    private void setIP() {
        try {
            // 获取Ip地址
            int ip1, ip2, ip3, ip4;
            ip1 = Integer.parseInt(mIp1ET.getText().toString().trim());
            ip2 = Integer.parseInt(mIp2ET.getText().toString().trim());
            ip3 = Integer.parseInt(mIp3ET.getText().toString().trim());
            ip4 = Integer.parseInt(mIp4ET.getText().toString().trim());

            // 校验Ip地址
            String ipStr = ip1 + "." + ip2 + "." + ip3 + "." + ip4;
            if (null == ipStr || "".equals(ipStr)) {
                showAlertDialog(getString(R.string.prompt),
                        getString(R.string.invalid_ip));
                return;
            } else {
                Pattern p = Pattern
                        .compile("(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})");
                Matcher m = p.matcher(ipStr);
                if (!m.matches()) {
                    showAlertDialog(getString(R.string.prompt),
                            getString(R.string.invalid_ip));
                    return;
                }
            }
            mApp.setServerIpStr(ipStr);
        } catch (Exception e) {
            showAlertDialog(getString(R.string.prompt),
                    getString(R.string.invalid_ip));
        }
    }
}
